<?php
	App::import('Model', 'Model');
	
	class TestLilFloat extends AppModel {
		var $name = 'TestLilFloat';
		var $cacheSources = false;
		var $useTable = 'lil_float';
		var $useDbConfig = 'test';
		var $actsAs = array('LilFloat');
	}
	
	class LilSearchCase extends CakeTestCase {
		var $Base = null;
		var $fixtures = array('app.lil_float');
	
		function start() {
			parent::start();
			$this->LilFloat =& new TestLilFloat();
		}
		
		function end() {
			parent::end();
		}
	
		function testIsValidFloat() {
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'10'), 0);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'10456'), 0);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'10,456'), 0);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'10.3'), 1);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'1023.3'), 1);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'1,023.3'), 1);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'18,023.3'), 1);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'185,023.3'), 1);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'10.321'), 3);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'1023.321'), 3);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'1,023.321'), 3);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'18,023.321'), 3);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'185,023.321'), 3);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'185023.321'), 3);
			$this->assertTrue($result);
			
			/* WRONG FORMAT */
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'10.32'), 1);
			$this->assertFalse($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'101.32'), 1);
			$this->assertFalse($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'10187.32'), 1);
			$this->assertFalse($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'10,187.32'), 1);
			$this->assertFalse($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'10187.'), 0);
			$this->assertFalse($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'10187.'), 1);
			$this->assertFalse($result);
		}
		
		function testIsValidFloatWithDifferentSeparators() {
			$this->LilFloat->setDecimalSeparator(',');
			$this->LilFloat->setThousandsSeparator('.');
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'1023,21'), 2);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'1.023,31'), 2);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'12.023,31'), 2);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'123.023,31'), 2);
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'123.345.023,31'), 2);
			$this->assertTrue($result);
		}
		
		function testIsValidFloatByField() {
			$this->LilFloat->setDecimalSeparator(',');
			$this->LilFloat->setThousandsSeparator('.');
			
			$result = $this->LilFloat->isValidFloat(array('amount'=>'1023,21'));
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('amount'=>'19.023,21'));
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('amount'=>'19.023,2'));
			$this->assertFalse($result);
			
			$result = $this->LilFloat->isValidFloat(array('amount'=>'19.023,213'));
			$this->assertFalse($result);
			
			$result = $this->LilFloat->isValidFloat(array('amount'=>'19.023'));
			$this->assertFalse($result);
			
			$result = $this->LilFloat->isValidFloat(array('amount'=>'19023'));
			$this->assertFalse($result);
		}
		
		function testIsValidFloatWithPosibilityOfHavingTwoDecimalPlaces() {
			$this->LilFloat->setDecimalSeparator(',');
			$this->LilFloat->setThousandsSeparator('.');
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'1023,21'), '1,2');
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'1.023,1'), '1,2');
			$this->assertTrue($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'1.023'), '1,2');
			$this->assertFalse($result);
			
			$result = $this->LilFloat->isValidFloat(array('floatfield'=>'1.023,123'), '1,2');
			$this->assertFalse($result);
		}
	}
?>